package com.demo.controller;

import com.demo.common.model.R;
import com.demo.config.query.ActivityQuery;
import com.demo.entity.Activity;
import com.demo.service.ActivityService;
import com.demo.vo.ActivityVo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/api/activity")
@RestController
public class ActivityController {
    @Autowired
    private ActivityService activityService;
    /**
     * 分页查询市场活动列表
     */
    @GetMapping("/page")
    public R<PageInfo<ActivityVo>> loadActivityPage(@RequestParam Integer current,
                                                    @RequestParam(required = false,defaultValue = "10") Integer pageSize,
                                                    ActivityQuery activityQuery
                                        ){
        PageInfo<ActivityVo> pageInfo =  activityService.queryActivityListByPage(current,pageSize,activityQuery);
        return R.OK(pageInfo);
    }

    /**
     * 添加市场活动列表
     */
    @PostMapping
    public R<String> loadAddActivity(@RequestBody Activity activity){
        boolean save = activityService.save(activity);
        return save ? R.OK(null) : R.FAIL("添加失败");
    }
    /**
     * 根据id 查询市场活动信息
     */
    @GetMapping("/info/{activityId}")
    public R<Activity> loadActivityInfo(@PathVariable("activityId") Integer activityId){
        Activity activity = activityService.getById(activityId);
        return R.OK(activity);
    }
    /**
     * 修改市场活动信息
     */
    @PutMapping
    public R<String> loadUpdateActivity(@RequestBody Activity activity){
        boolean result = activityService.updateById(activity);
        return  result ? R.OK(null) : R.FAIL("修改失败");
    }
    /**
     * 删除市场活动信息
     */
    @DeleteMapping("/{ids}")
    public R<String> removeActivity(@PathVariable("ids") List<Integer> ids){
        boolean result = activityService.removeByIds(ids);
        return result ? R.OK(null) :R.FAIL("删除失败");
    }
    /**
     * 根据标识查看市场活动详情
     */
    @GetMapping("/detail/{activityId}")
    public R<ActivityVo> loadDetailActivity(@PathVariable("activityId") Integer activityId){
        ActivityVo activityVo = activityService.queryActivityDetailById(activityId);
        return R.OK(activityVo);
    }
    /**
     * 查询所有市场活动列表
     */
    @GetMapping("/list")
    public R<List<Activity>> loadAllActivityList(){
        List<Activity> list = activityService.queryAllActivityList();
        return R.OK(list);
    }
}
